package com.smile.config;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.useragent.Platform;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentParser;
import com.smile.commons.action.ActionUtils;
import com.smile.commons.ip.IpUtils;
import com.smile.commons.md5.MD5Util;
import com.smile.commons.request.RequestIdUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Configuration
public class AccessInterceptor extends HandlerInterceptorAdapter {
    @Value("${spring.application.name}")
    private String appName;

    @Value("${spring.redis.host}")
    private String redisHost;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        if (StpUtil.isLogin()) {
//            StpUtil.updateLastActiveToNow();
//        }

        System.out.println("进入到 AccessInterceptor中");
        System.out.println("accessInterceptor: " + request.getRequestURI());
        System.out.println(redisHost);
        if (StrUtil.isNotBlank(request.getHeader("request_id"))) {
            String requestId = request.getHeader("request_id");
            RequestIdUtils.set(requestId);
            log.info("当前模块的名字是: {},request_id={}", appName, requestId);
            if (StpUtil.isLogin()) {
                log.info("当前的用户id为:{},request_id={}", StpUtil.getLoginId(), requestId);
            }
        }
//        Enumeration<String> headerNames = request.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String headerName = headerNames.nextElement();
//            String headerValue = request.getHeader(headerName);
//            System.out.println("headerName: " + headerName + ", headerValue: " + headerValue);
//        }

        String header = request.getHeader("User-Agent");
        if (StrUtil.isNotBlank(header) || !"ApipostRuntime/1.1.0".equals(header)) {

//            Map<String, String> result = UserAgentParser.parseUserAgent(header);
            UserAgent parse = UserAgentParser.parse(header);
            Platform platform = parse.getPlatform();
            String os = parse.getOsVersion();
            String browser = parse.getBrowser().getName();
            ActionUtils.setIpInfo(IpUtils.getIpAddress(request));
            ActionUtils.setSystemInfo(platform + " " + os);
            ActionUtils.setExplorerInfo(browser);
        } else {
            ActionUtils.setIpInfo(IpUtils.getIpAddress(request));
            ActionUtils.setSystemInfo("APIPOST");
            ActionUtils.setExplorerInfo("APIPOST");
        }


//        if ("/api/auth/sso/doLogin".equals(request.getRequestURI())) {
////TODO 这里为了用apipost调试暂时注掉
//            System.out.println(request.getRemoteHost());
//            System.out.println(IpUtils.getIpAddress(request));
//            String explorer = request.getHeader("User-Agent");
//            System.out.println(explorer);
//            String[] split = explorer.split("\\)");
//            System.out.println("===========");
//            System.out.println(split[2]);
//            System.out.println(split[2].substring(1, split[2].length()));
//            String s = explorer.split("\\(")[1].split("\\)")[0].split(";")[0] + explorer.split("\\(")[1].split("\\)")[0].split(";")[2];
//
//            System.out.println(s);
//            String ipAddress = IpUtils.getIpAddress(request);
//
//
//
//            ActionUtils.setIpInfo(IpUtils.getIpAddress(request));
//            ActionUtils.setExplorerInfo(split[2].substring(1, split[2].length()));
//            ActionUtils.setSystemInfo(s);
//        }

        return true;
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.md5WithSalt("django", "nice"));
    }

}
